package com.qubuyer.business.mine.viewmodel

import androidx.lifecycle.MutableLiveData
import com.qubuyer.business.auth.model.AuthModel
import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.framework.BaseViewModel
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.repository.entitys.MineCardEntity
import com.qubuyer.repository.entitys.UserEntity
import com.qubuyer.utils.SessionUtil

open class UserViewModel : BaseViewModel() {
    val userEntity = MutableLiveData(SessionUtil.getInstance().userInfo)

    @AutoInject
    val authModel = AuthModel()

    fun getUserInfo() {
        RetrofitCall<UserEntity?>(lifecycleOwner, this) {
            api { authModel.userInfo() }
            success {
                userEntity.value = it
            }
        }
    }

    fun userInfoEdit(nickname: String, block: () -> Unit) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { authModel.userInfoEdit(nickname) }
            success {
                block.invoke()
            }
        }
    }

    fun bindThirdAccount(oauth: String, openId: String, block: () -> Unit) {
        RetrofitCall<MineCardEntity?>(lifecycleOwner, this) {
            api { authModel.codeBind(oauth, openId) }
            success {
                block.invoke()
            }
        }
    }

    fun getAliPayParams(block: (String) -> Unit) {
        RetrofitCall<String?>(lifecycleOwner, this) {
            api { authModel.alipayInitApp() }
            success {
                block.invoke(it ?: "")
            }
        }
    }
}